package com.wzc.bigevent.service.impl;

import com.wzc.bigevent.mapper.UserMapper;
import com.wzc.bigevent.pojo.Result;
import com.wzc.bigevent.pojo.User;
import com.wzc.bigevent.dto.UserPwdDTO;
import com.wzc.bigevent.service.UserService;
import com.wzc.bigevent.utils.JwtUtil;
import com.wzc.bigevent.utils.Md5Util;
import com.wzc.bigevent.utils.ThreadLocalUtil;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.wzc.bigevent.utils.RedisContext.*;


@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 根据用户名查询用户
     * @param username
     * @return
     */
    @Override
    public User findByUserName(String username) {
        return userMapper.findByUserName(username);
    }


    /**
     * 注册用户
     * @param username
     * @param password
     */
    @Override
    public void register(String username, String password) {
        userMapper.save(username, Md5Util.getMD5String(password));
    }

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public Result<String> login(String username, String password) {
        //1. 根据用户名查询用户
        User u = userMapper.findByUserName(username);
        //2. 判断是否存在
        if(u == null) {
            return Result.error("用户名错误");
        }
        //3. 判断密码是否正确
        if(!u.getPassword().equals(Md5Util.getMD5String(password))) {
            return Result.error("密码错误");
        }
        //4. 生成JWT令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", u.getId());
        claims.put("username", u.getUsername());
        String Token = JwtUtil.genToken(claims);

        // 5. 将Token存入Redis
        stringRedisTemplate.opsForValue().set(LOGIN_TOKEN_KEY + u.getId(), Token, LOGIN_TOKEN_TTL, TimeUnit.MINUTES);

        return Result.success(Token);
    }

    /**
     * 获取用户信息
     * @param
     * @return
     */
    @Override
    public Result<User> userInfo() {
        //1. 利用ThreadLocal获取用户信息
        Map<String,Object> map = ThreadLocalUtil.get();

        //2. 根据用户名查询用户
        String username = (String) map.get("username");
        User user = userMapper.findByUserName(username);

        //3. 返回结果
        return Result.success(user);
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @Override
    public void update(User user) {
        userMapper.update(user);
    }

    /**
     * 更新用户头像
     * @param avatarUrl
     */
    @Override
    public void updateAvatar(String avatarUrl) {
        // 获取登录用户id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id =(Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    /**
     * 更新用户密码
     * @param userPwdDTO
     */
    @Override
    public Result updatePwd(UserPwdDTO userPwdDTO) {
        //1. 利用ThreadLocal获取用户信息
        Map<String, Object> map = ThreadLocalUtil.get();

        //2. 根据用户名查询用户
        String username = (String) map.get("username");
        User user = userMapper.findByUserName(username);

        //3. 判断密码是否正确
        if (!user.getPassword().equals(Md5Util.getMD5String(userPwdDTO.getOldPwd()))) {
            return Result.error("原密码错误");
        }
        if (!userPwdDTO.getNewPwd().equals(userPwdDTO.getRePwd())) {
            return Result.error("两次密码不一致");
        }
        //4. 更新密码
        userMapper.updatePwd(Md5Util.getMD5String(userPwdDTO.getNewPwd()),user.getId());

        //5. 删除Redis中的Token
        stringRedisTemplate.delete(LOGIN_TOKEN_KEY + user.getId());
        return Result.success();
    }
}
